package com.hc.app.frequencycontrol;

import com.hc.app.dto.FrequencyControlDto;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 频控策略工厂
 */
public class FrequencyStrategyFactory {
    static Map<String, AbstractFrequencyControl<?>> frequencyMap = new ConcurrentHashMap<>(8);

    /**
     * 集合中注册频控策略
     * @param frequencyName
     * @param frequency
     * @param <K>
     */
    public static <K extends FrequencyControlDto> void registFrequency(String frequencyName, AbstractFrequencyControl<K> frequency){
        frequencyMap.put(frequencyName,frequency);
    }

    /**
     * 根据频控的名称获取频控策略
     * @param frequencyName
     * @return
     * @param <K>
     */
    public static <K extends FrequencyControlDto> AbstractFrequencyControl<K> getFrequencyByName(String frequencyName){
        return (AbstractFrequencyControl<K>) frequencyMap.get(frequencyName);
    }

    private FrequencyStrategyFactory() {

    }

}
